$(function() {
  //监听val变化
  $(".log-item input").on('input propertychange focus', function() {
    var _this = $(this);
    if ($(this).val() != '') {
      setTimeout(function() {
        $(_this).siblings(".loginIcon").show();
      }, 200);
    } else {
      $(this).siblings(".loginIcon").removeAttr("style");
    }
  });
  $(".log-item input").on("blur", function() {
    var _this = $(this);
    setTimeout(function() {
      $(_this).siblings(".loginIcon").removeAttr("style");
    }, 200)
  });
  //清空输入框
  $(".log-item .loginIcon").on("click", function() {
    event.preventDefault();
    $(this).siblings("input").val('');
    $(this).hide();
  });
});

$(document).keyup(function(event) {
  if (event.keyCode == 13) { //enter
    login();
  }
});
var pageUrl = window.location.search;
pageUrl = pageUrl.substring(9, pageUrl.length);

function login() {
  var adminName = $('#adminName').val();
  var passward = $('#passward').val();
  if (adminName.trim().length == 0 || passward.length == 0) {
    $('.log-error').html("用户名或密码不能为空");
    return false;
  }
  $.ajax({
    url: site.apiUrl + "/admin/login",
    type: "post",
    async: true,
    dataType: "JSON",
    ContentType: "application/json",
    data: {
      'tel': adminName,
      'passward': passward,
    },
    beforeSend: function(request) {
      request.setRequestHeader("ClientIdentity_MT", "_PC");
    },
    success: function(data) {
      // console.log(data)
      if (data.code == 200) {
        window.localStorage.setItem("backstage-access-token", data.data.token);
        window.localStorage.setItem("backstage-access-name", data.data.name); // 姓名/昵称
        window.localStorage.setItem("backstage-access-adminName", data.data.adminName); // 登录账号
        window.localStorage.setItem("backstage-access-userId", data.data.id);
        window.localStorage.setItem("backstage-access-ifSuperMan", data.data.ifSuperMan);
        window.localStorage.setItem("backstage-access-webSite", data.data.webSite); // 服务网点Id

        if (data.data.ifSuperMan == 1) {
          window.localStorage.setItem("backstage-access-permissionList", JSON.stringify(permissionList));
        } else {
          window.localStorage.setItem("backstage-access-permissionList", JSON.stringify(permissionList2));
        }

        if (pageUrl == '') {
          window.location.href = "index.html";
        } else {
          window.location.href = pageUrl;
        }
      } else {
        $('.log-error').html(data.msg);
      }
    },
    error: function(err) {
      console.log(err)
    }
  });
  // window.location.href = "index.html";
}